//验证身份证
var IDreg = /^(^[1-9]\d{7}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}$)|(^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])((\d{4})|\d{3}[Xx])$)$/;
//	手机号正则
var phoneReg = /^[1][3,4,5,7,8,6,9][0-9]{9}$/;
//验证用户名
function checkAccount(value) { //value：表单的值、item：表单的DOM对象
	if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
		layer.msg('请填写有效的用户名！');
		return false;
	}
	if(/(^\_)|(\__)|(\_+$)/.test(value)) {
		layer.msg('用户名首尾不能出现下划线\'_\'');
		return false;
	}
	if(/^\d+\d+\d$/.test(value)) {
		layer.msg('用户名不能全为数字');
		return false;
	}
	return true;
}

//点击注册按钮
$("#zc-regist").click(function() {
	var flag=checkForm();
	if(flag==true) {
		ajaxForm();
	} else {
		return false
	}
})
//$('.zc-account').blur(function(e) {
//	e.value = value.replace(/[^\w\.\/]/ig, '');
//});
$("#teacherRegist").validate({
	onfocusout: function(element) {
		$(element).valid();
	},
	rules: {
		password: {
			minlength: 5
		},
		confirm_password: {
			minlength: 5,
			equalTo: "#password"
		}
	},
	messages: {
		password: {
			required: "请输入密码",
			minlength: "密码长度不能小于 5 个字母"
		},
		confirm_password: {
			required: "请输入密码",
			minlength: "密码长度不能小于 5 个字母"
		}
	}
});

//点击提交,进行判断
function checkForm() {
	var account = $('.zc-account').val(); //账号
	var name = $(".zc-username").val(); //姓名
	//	var age=$('.zc-age').val();//年龄
		var gender = $(".zc-xbxz:checked").val(); //性别
	var birthday = $('.birthday').val(); //出生日期
	//	var school = $('.zc-school').val(); //毕业院校
	var number = $('.number').val()
	var idCard = $('.zcsfz').val(); //身份证
	var liaison = $(".zc-phone").val(); //手机号码
	//	var head="111";
		var profession = $('#profession option:selected').val();//专业
	var category = $(".category option:selected").val();//类别
	var password = $('.zc-password').val(); //密码
	var confirmVal = $('.confirm-password').val(); //确认密码
	// var rank = $('.faculty-select option:selected').val(); //职级
	var unit = $(".unit").val();

	if(!checkAccount(account)) {
		return false
	}
	if(!name) {
		layer.msg('姓名不能为空!');
		$(".zc-username").focus();
		return false;
	}
	if(!gender){
		layer.msg("请选择性别!");
		return false;
	}
	if(!unit) {
		layer.msg("工作单位不能为空!");
		return false;
	}
	if(!IDreg.test(idCard)) {
		layer.msg("身份证号码格式不正确!");
		$('.zcsfz').focus();
		return false;
	}
	if(!phoneReg.test(liaison)) {
		layer.msg("手机号码格式错误!");
		$(".zc-phone").focus();
		return false;
	}
	if(!number) {
		layer.msg('人员编号不能为空!');
		$('.number').focus();
		return false;
	}
	if(!profession){
		layer.msg("专业不能为空!");
		return false;
	}
	if(!category){
		layer.msg("类别不能为空!");
		return false;
	}
//  if(!rank) {
//      layer.msg("职级不能为空!");
//      return false;
//  }
	//	if(!head){layer.msg("请上传头像!")}
	if(!password) {
		layer.msg("密码为空!");
		$('.zc-password').focus();
		return false;
	}
	if (password.length <= 5) {
		layer.msg("密码长度最少6位!");
		$('.zc-password').focus();
		return false;
	}
	if(password !== confirmVal) {
		layer.msg("两次输入的密码不一致!");
		$('.confirm-password').focus();
		return false;
	}
	return true;
}
//提交注册
function ajaxForm() {
	//	输入框
	var account = $('.zc-account').val(); //账号
	var name = $(".zc-username").val(); //姓名
	var birthday = $('.birthday').val(); //出生日期
	var idCard = $('.zcsfz').val(); //身份证
	var liaison = $(".zc-phone").val(); //手机号码
	var password = $('.zc-password').val(); //密码
	var confirmVal = $('.confirm-password').val(); //确认密码
	var rank = $(".rank").val(); //职级
	var number = $('.number').val()
	var unit = $(".unit").val();
	var category = $(".category option:selected").val();
	var profession = $(".profession option:selected").val();
	var stamp = new Date().getTime();
	var gender=$("input[name='gender']:checked").val();
	
	var param1 = {
		name: name,
		birthday: birthday,
		idCard: idCard,
		liaison: liaison,
		number:number,
		unit: unit,
		account: account,
		password: password,
		category: category,
		profession: profession,
		rank: rank
	}
	var param2 = Object.assign(publicParam, param1, {
		stamp: stamp,gender:gender
	})
	var param = JSON.stringify(param2);
	var url = path + "/teacher/registTeacher";

	$.ajax({
		type: "post",
		url: url,
		dataType: "json",
		data: {param: param},
		success: function(data) {
			if(data.status == '000') {
				alert("恭喜你注册成功!等待审核后进行登录....")
				// layer.msg("恭喜你注册成功!等待审核后进行登录...." , { time: 2000} );
				window.location.href = "login.html";
			} else if(data.status == '001') {
				layer.msg(data.rows)
			}else if(data.status == '006'){
				layer.msg("账号正在审核中!")
			}else if(data.status == '005'){
				layer.msg("后台处理异常!")
			}
		},
		error: function() {
			layer.msg('网络异常!稍后再进行注册')
		}
	});
}